﻿/*
    Copyright (C) 2008, wj2929, wj2929@gmail.com

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    *KEEP THIS HEADER INFORMATION*
*/

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using mjRegister.Entity;
using System.Threading;
using mjRegister.BAL;
using System.IO;

namespace mjRegister
{
    public partial class FriendsManage : Form
    {
        private Form2 _ownerForm;
        private AccountInfo _accountInfo;

        IList<Thread> lstThread = new List<Thread>();

        private ListViewColumnSorter lvwColumnSorter;

        bool form_load_display_friends_flag = false;

        IList<SimpleUserInfo> allFriends = new List<SimpleUserInfo>();

        private CarInfo _sendCarInfo;

        IList<SimpleUserInfo> selectUserInfos = new List<SimpleUserInfo>();

        bool msg_groupsend_flag = false;

        public string Send_Msg_Str = string.Empty;

        public FriendsManage(Form2 ownerForm, AccountInfo accountInfo) : this(ownerForm,accountInfo,null)
        {
            //InitializeComponent();

            //lvwColumnSorter = new ListViewColumnSorter(0,1);
            //this.listView1.ListViewItemSorter = lvwColumnSorter;

            //_ownerForm = ownerForm;
            //_accountInfo = accountInfo;
        }

        public FriendsManage(Form2 ownerForm, AccountInfo accountInfo,CarInfo sendCarInfo)
        {
            InitializeComponent();

            lvwColumnSorter = new ListViewColumnSorter(0,1);
            this.listView1.ListViewItemSorter = lvwColumnSorter;

            _sendCarInfo = sendCarInfo;
            _ownerForm = ownerForm;
            _accountInfo = accountInfo;
        }

        void LoadUserGroups()
        {
            User user = new User();

            this.Invoke(new DAppendText(_ownerForm.AppendText), (string.Format("[{0}]：获取[{1}]群组列表：\n",
                DateTime.Now.ToString("HH:mm:ss"),
                string.Format("{0},{1}", _accountInfo.UserInfo.uid, _accountInfo.UserInfo.real_name))));

            this.Invoke(new DAppendText(_ownerForm.AppendText), (string.Format("[{0}]：[{1}]开始登录系统……",
                DateTime.Now.ToString("HH:mm:ss"),
                string.Format("{0}", _accountInfo.Email))));
            if (user.Login(_accountInfo))
            {
                this.Invoke(new DAppendText(_ownerForm.AppendText), (string.Format("[{0}]：[{1}]登录系统成功！\n",
                    DateTime.Now.ToString("HH:mm:ss"),
                    string.Format("{0},{1}", _accountInfo.UserInfo.uid, _accountInfo.UserInfo.real_name))));

                this.Invoke(new DAppendText(_ownerForm.AppendText), (string.Format("[{0}]：[{1}]开始获取群组列表……",
                    DateTime.Now.ToString("HH:mm:ss"),
                    string.Format("{0},{1}", _accountInfo.UserInfo.uid, _accountInfo.UserInfo.real_name))));

                IList<GroupInfo> groupInfos = user.GetGroupInfos();
                groupInfos.Insert(0, new GroupInfo { Name = "全部好友" });

                this.Invoke(new DAppendText(_ownerForm.AppendText), (string.Format("[{0}]：[{1}]获取群组列表完成！\n",
                    DateTime.Now.ToString("HH:mm:ss"),
                    string.Format("{0},{1}", _accountInfo.UserInfo.uid, _accountInfo.UserInfo.real_name))));

                this.Invoke(new DFillDDLGroup(FillDDLGroup), groupInfos);

                user.Logout();

                this.Invoke(new DAppendText(_ownerForm.AppendText),
                    string.Format("[{0}]：[{1}]退出系统！\n",
                        DateTime.Now.ToString("HH:mm:ss"),
                        string.Format("{0},{1}", _accountInfo.UserInfo.uid, _accountInfo.UserInfo.real_name)));
            }
        }

        delegate void DFillDDLGroup(IList<GroupInfo> groupInfos);
        void FillDDLGroup(IList<GroupInfo> groupInfos)
        {
            this.ddlgroup.Items.Clear();

            for (int i = 0; i < groupInfos.Count; i++)
            {
                this.ddlgroup.Items.Add(groupInfos[i].Name);
            }

            this.ddlgroup.SelectedIndex = 0;
        }


        delegate void DFillListView(IList<SimpleUserInfo> userInfos);

        void FillListView(IList<SimpleUserInfo> userInfos)
        {
            this.listView1.Items.Clear();

            IList<ListViewItem> listViewItems = new List<ListViewItem>();

            for (int i = 0; i < userInfos.Count; i++)
            {
                SimpleUserInfo userInfo = userInfos[i];

                AccountInfo accountInfo = null;
                if(_ownerForm.htAccountInfoByUid[userInfo.uid] != null)
                    accountInfo = (AccountInfo)_ownerForm.htAccountInfoByUid[userInfo.uid];

                ListViewItem item = new ListViewItem(
                    new string[] { 
                        (i + 1).ToString(),
                        userInfo.uid, 
                        userInfo.real_name, 
                        accountInfo == null ? 
                            string.Empty :
                            accountInfo.ActiveStatus != ActiveStatus.AlreadyActive ? "未验证激活状态或账户密码错误！" :
                                accountInfo.AccountGroupInfo == null ? string.Empty : 
                                    string.Format("{0},{1},{2}",accountInfo.AccountGroupInfo.Name,accountInfo.IsMaster ? "主号" : "小号",accountInfo.Email)});

                if (accountInfo != null && accountInfo.ActiveStatus == ActiveStatus.AlreadyActive)
                    item.Tag = accountInfo;
                else
                    item.Tag = userInfo;

                listViewItems.Add(item);
            }

            this.listView1.Items.AddRange(GetArray<ListViewItem>.Get(listViewItems));

            this.label1.Text = string.Format("共{0}位好友。", userInfos.Count.ToString());
        }

        private void listView1_ColumnClick(object sender, ColumnClickEventArgs e)
        {
            // 检查点击的列是不是现在的排序列.
            if (e.Column == lvwColumnSorter.SortColumn)
            {
                // 重新设置此列的排序方法.
                if (lvwColumnSorter.Order == SortOrder.Ascending)
                {
                    lvwColumnSorter.Order = SortOrder.Descending;
                }
                else
                {
                    lvwColumnSorter.Order = SortOrder.Ascending;
                }
            }
            else
            {
                // 设置排序列，默认为正向排序
                lvwColumnSorter.SortColumn = e.Column;
                lvwColumnSorter.Order = SortOrder.Ascending;
            }

            // 用新的排序方法对ListView排序
            this.listView1.Sort();

        }

        SimpleUserInfo GetUserInfoFromListViewItem(ListViewItem listViewItem)
        {
            SimpleUserInfo simpleUserInfo = null;

            if (listViewItem.Tag.GetType() == typeof(SimpleUserInfo))
            {
                simpleUserInfo = (SimpleUserInfo)listViewItem.Tag;
            }
            else if (listViewItem.Tag.GetType() == typeof(AccountInfo))
                simpleUserInfo = User.GetSimpleUserInfoFromUserInfo(((AccountInfo)listViewItem.Tag).UserInfo);

            return simpleUserInfo;
        }

        private void button3_Click(object sender, EventArgs e)
        {
            IList<SimpleUserInfo> userInfos = new List<SimpleUserInfo>();

            for (int i = 0; i < this.listView1.Items.Count; i++)
            {
                if (this.listView1.Items[i].Checked)
                {
                    userInfos.Add(GetUserInfoFromListViewItem(this.listView1.Items[i]));
                }
            }
            if (userInfos.Count == 0)
            {
                MessageBox.Show("尚未选择项！");
                return;
            }
            if (MessageBox.Show(string.Format("确定删除选择的{0}位好友？", userInfos.Count.ToString()), "请确定", MessageBoxButtons.OKCancel) == DialogResult.OK)
            {
                Thread t = new Thread(new ParameterizedThreadStart(DeleteFriend));
                t.Start(userInfos);
                lstThread.Add(t);
            }
        }

        delegate void DAppendText(string text);

        void DeleteFriend(object obj)
        {
            this.Invoke(new DSetControlDisabled(SetControlDisabled));

            this.Invoke(new DAppendText(_ownerForm.AppendText),
                string.Format("[{0}]：[{1}]开始好友删除……\n",
                    DateTime.Now.ToString("HH:mm:ss"),
                    string.Format("{0},{1}", _accountInfo.UserInfo.uid, _accountInfo.UserInfo.real_name)));

            IList<SimpleUserInfo> userInfos = (List<SimpleUserInfo>)obj;
            User user = new User();
            this.Invoke(new DAppendText(_ownerForm.AppendText),
                string.Format("[{0}]：[{1}]开始登录系统……",
                    DateTime.Now.ToString("HH:mm:ss"),
                    string.Format("{0},{1}", _accountInfo.UserInfo.uid, _accountInfo.UserInfo.real_name)));
            if (user.Login(_accountInfo))
            {
                this.Invoke(new DAppendText(_ownerForm.AppendText),
                    string.Format("[{0}]：[{1}]登录系统成功！\n",
                        DateTime.Now.ToString("HH:mm:ss"),
                        string.Format("{0},{1}", _accountInfo.UserInfo.uid, _accountInfo.UserInfo.real_name)));
                for (int i = 0; i < userInfos.Count; i++)
                {
                    SimpleUserInfo userInfo = (SimpleUserInfo)userInfos[i];
                    user.DeleteFriend(userInfo);
                    this.Invoke(new DAppendText(_ownerForm.AppendText), string.Format("[{0}]：删除好友进度：({1},{2})，{3}", 
                            DateTime.Now.ToString("HH:mm:ss"),
                            userInfo.uid, 
                            userInfo.real_name, 
                            string.Format("{0}/{1}", (i + 1).ToString(), userInfos.Count.ToString())));
                    if (i + 1 < userInfos.Count)
                        Thread.Sleep(_ownerForm.delayConnect * 1000);
                }
                user.Logout();
                this.Invoke(new DAppendText(_ownerForm.AppendText),
                    string.Format("\n[{0}]：[{1}]退出系统！\n",
                        DateTime.Now.ToString("HH:mm:ss"),
                        string.Format("{0},{1}", _accountInfo.UserInfo.uid, _accountInfo.UserInfo.real_name)));
            }
            this.Invoke(new DAppendText(_ownerForm.AppendText), string.Format("[{0}]：完成好友删除！", DateTime.Now.ToString("HH:mm:ss")));
            this.Invoke(new DReLoadUserInfos(ReLoadUserInfos));
            this.Invoke(new DSetControlDisabled(RestoreControlEnabled));
        }

        void IterationDeleteFriend(object obj)
        {
            this.Invoke(new DSetControlDisabled(SetControlDisabled));

            this.Invoke(new DAppendText(_ownerForm.AppendText),
                string.Format("[{0}]：[{1}]级联删除好友：\n",
                    DateTime.Now.ToString("HH:mm:ss"),
                    string.Format("{0},{1}", _accountInfo.UserInfo.uid, _accountInfo.UserInfo.real_name)));

            IList<SimpleUserInfo> userInfos = (List<SimpleUserInfo>)obj;
            User user = new User();
            this.Invoke(new DAppendText(_ownerForm.AppendText),
                string.Format("[{0}]：[{1}]开始登录系统……",
                    DateTime.Now.ToString("HH:mm:ss"),
                    string.Format("{0},{1}", _accountInfo.UserInfo.uid, _accountInfo.UserInfo.real_name)));
            if (user.Login(_accountInfo))
            {
                this.Invoke(new DAppendText(_ownerForm.AppendText),
                    string.Format("[{0}]：[{1}]登录系统成功！\n",
                        DateTime.Now.ToString("HH:mm:ss"),
                        string.Format("{0},{1}", _accountInfo.UserInfo.uid, _accountInfo.UserInfo.real_name)));

                for (int i = 0; i < userInfos.Count; i++)
                {
                    SimpleUserInfo userInfo = (SimpleUserInfo)userInfos[i];
                    if (_ownerForm.htAccountInfoByUid[userInfo.uid] != null)
                    {
                        this.Invoke(new DAppendText(_ownerForm.AppendText),
                            string.Format("[{0}]：好友[{1}]受本工具管理！开始删除它的所有好友：",
                                DateTime.Now.ToString("HH:mm:ss"),
                                string.Format("{0},{1}", userInfo.uid, userInfo.real_name)));

                        AccountInfo userAccountInfo = (AccountInfo)_ownerForm.htAccountInfoByUid[userInfo.uid];
                        User user1 = new User();

                        this.Invoke(new DAppendText(_ownerForm.AppendText),
                            string.Format("[{0}]：好友[{1}]开始登录系统……",
                                DateTime.Now.ToString("HH:mm:ss"),
                                string.Format("{0},{1}", userInfo.uid, userInfo.real_name)));

                        if (user1.Login(userAccountInfo))
                        {
                            userAccountInfo.FriendInfos = user1.GetAllUserInfos();
                            Account.StaticFriends(userAccountInfo);

                            this.Invoke(new DAppendText(_ownerForm.AppendText),
                                string.Format("[{0}]：好友[{1}]登录系统成功，并重新获取好友列表，缓存到本地！\n",
                                    DateTime.Now.ToString("HH:mm:ss"),
                                    string.Format("{0},{1}", userInfo.uid, userInfo.real_name)));

                            for (int j = 0; j < userAccountInfo.FriendInfos.Count; j++)
                            {
                                SimpleUserInfo userInfo1 = userAccountInfo.FriendInfos[j];
                                user1.DeleteFriend(userInfo1);

                                this.Invoke(new DAppendText(_ownerForm.AppendText), string.Format("[{0}]：[{1}]删除好友进度：({2})，{3}(级联) {4}(总)",
                                        DateTime.Now.ToString("HH:mm:ss"),
                                        string.Format("{0},{1}", userInfo.uid, userInfo.real_name),
                                        string.Format("{0},{1}", userInfo1.uid, userInfo1.real_name),
                                        string.Format("{0}/{1}", (j + 1).ToString(), userAccountInfo.FriendInfos.Count.ToString()),
                                        string.Format("{0}/{1}", (i + 1).ToString(), userInfos.Count.ToString())));
                                if (j + 1 < userAccountInfo.FriendInfos.Count)
                                    Thread.Sleep(_ownerForm.delayConnect * 1000);
                            }

                            userAccountInfo.FriendInfos = user1.GetAllUserInfos();
                            Account.StaticFriends(userAccountInfo);

                            user1.Logout();
                            this.Invoke(new DAppendText(_ownerForm.AppendText),
                                string.Format("\n[{0}]：[{1}]删除所有好友完成，退出系统！\n",
                                    DateTime.Now.ToString("HH:mm:ss"),
                                    string.Format("{0},{1}", userInfo.uid, userInfo.real_name)));
                        }
                    }
                    else
                    {
                        user.DeleteFriend(userInfo);
                        this.Invoke(new DAppendText(_ownerForm.AppendText), string.Format("[{0}]：[{1}]删除好友进度：({2})，{3}(级联) {4}(总)\n",
                                DateTime.Now.ToString("HH:mm:ss"),
                                string.Format("{0},{1}", _accountInfo.UserInfo.uid, _accountInfo.UserInfo.real_name),
                                string.Format("{0},{1}", userInfo.uid, userInfo.real_name),
                                "1/1",
                                string.Format("{0}/{1}", (i + 1).ToString(), userInfos.Count.ToString())));
                    }
                    if (i + 1 < userInfos.Count)
                        Thread.Sleep(_ownerForm.delayConnect * 1000);
                }
                user.Logout();
                this.Invoke(new DAppendText(_ownerForm.AppendText),
                    string.Format("[{0}]：[{1}]退出系统！\n",
                        DateTime.Now.ToString("HH:mm:ss"),
                        string.Format("{0},{1}", _accountInfo.UserInfo.uid, _accountInfo.UserInfo.real_name)));
            }
            this.Invoke(new DAppendText(_ownerForm.AppendText), string.Format("[{0}]：完成好友级联删除！", DateTime.Now.ToString("HH:mm:ss")));
            this.Invoke(new DReLoadUserInfos(ReLoadUserInfos));
            this.Invoke(new DSetControlDisabled(RestoreControlEnabled));
        }
 

        delegate void DReLoadUserInfos();
        void ReLoadUserInfos()
        {
            this.button4_Click(this.button4, EventArgs.Empty);
        }

        private void FriendsManage_FormClosing(object sender, FormClosingEventArgs e)
        {
            Stop();
        }

        public void Stop()
        {
            lstThread = lstThread.Where(t => t.IsAlive).ToList();

            if (lstThread.Count > 0)
            {
                this.Invoke(new DAppendText(_ownerForm.AppendText),
                    string.Format("[{0}]：正在中断当前执行线程……",
                        DateTime.Now.ToString("HH:mm:ss")));

                for (int i = 0; i < lstThread.Count; i++)
                {
                    lstThread[i].Abort();
                }

                this.Invoke(new DAppendText(_ownerForm.AppendText),
                    string.Format("[{0}]：中断当前执行线程完成！\n",
                        DateTime.Now.ToString("HH:mm:ss")));
            }
        }

        void FetchFriends()
        {
            this.Invoke(new DSetControlDisabled(SetControlDisabled));

            User user = new User();
            this.Invoke(new DAppendText(_ownerForm.AppendText),(string.Format("[{0}]：重新获取[{1}]好友列表……\n",
                DateTime.Now.ToString("HH:mm:ss"),
                string.Format("{0},{1}", _accountInfo.UserInfo.uid, _accountInfo.UserInfo.real_name))));

            this.Invoke(new DAppendText(_ownerForm.AppendText),(string.Format("[{0}]：[{1}]开始登录系统……",
                DateTime.Now.ToString("HH:mm:ss"),
                string.Format("{0},{1}", _accountInfo.UserInfo.uid, _accountInfo.UserInfo.real_name))));
            if (user.Login(_accountInfo))
            {
                this.Invoke(new DAppendText(_ownerForm.AppendText),(string.Format("[{0}]：[{1}]登录系统成功！",
                    DateTime.Now.ToString("HH:mm:ss"),
                    string.Format("{0},{1}", _accountInfo.UserInfo.uid, _accountInfo.UserInfo.real_name))));

                allFriends = user.GetAllUserInfos();

                this.Invoke(new DAppendText(_ownerForm.AppendText),(string.Format("[{0}]：[{1}]获取好友列表，并缓存本地！",
                    DateTime.Now.ToString("HH:mm:ss"),
                    string.Format("{0},{1}", _accountInfo.UserInfo.uid, _accountInfo.UserInfo.real_name))));

                _accountInfo.FriendInfos = allFriends;
                Account.StaticFriends(_accountInfo);

                this.Invoke(new DFillListView(FillListView),allFriends);

                user.Logout();

                this.Invoke(new DAppendText(_ownerForm.AppendText),(string.Format("[{0}]：[{1}]成功退出系统！\n",
                    DateTime.Now.ToString("HH:mm:ss"),
                    string.Format("{0},{1}", _accountInfo.UserInfo.uid, _accountInfo.UserInfo.real_name))));
            }

            this.Invoke(new DAppendText(_ownerForm.AppendText),(string.Format("[{0}]：重新获取[{1}]好友列表完成！\n",
                DateTime.Now.ToString("HH:mm:ss"),
                string.Format("{0},{1}", _accountInfo.UserInfo.uid, _accountInfo.UserInfo.real_name))));

            this.Invoke(new DCompleteFetchFriends(CompleteFetchFriends));

            this.Invoke(new DSetControlDisabled(RestoreControlEnabled));
        }

        delegate void DCompleteFetchFriends();
        void CompleteFetchFriends()
        {
            this.label1.Text = string.Format("获取好友列表完成，共{0}名好友",this.listView1.Items.Count.ToString());
        }

        private void button4_Click(object sender, EventArgs e)
        {
            this.label1.Text = "请稍等，正在获取好友列表中……";
            Thread t = new Thread(new ThreadStart(FetchFriends));
            t.Start();
            lstThread.Add(t);
        }

        private void button5_Click(object sender, EventArgs e)
        {
            this.listView1.Items.Clear();
            IList<ListViewItem> listViewItems = new List<ListViewItem>();
            for (int i = 0; i < allFriends.Count; i++)
            {
                SimpleUserInfo userInfo = allFriends[i];
                if (userInfo.real_name.Contains(this.textBox1.Text))
                {
                    AccountInfo accountInfo = null;
                    if (_ownerForm.htAccountInfoByUid[userInfo.uid] != null)
                        accountInfo = (AccountInfo)_ownerForm.htAccountInfoByUid[userInfo.uid];

                    ListViewItem item = new ListViewItem(
                        new string[] { 
                        userInfo.uid, 
                        userInfo.real_name, 
                        accountInfo == null ? 
                            string.Empty :
                            accountInfo.ActiveStatus != ActiveStatus.AlreadyActive ? "未验证激活状态或账户密码错误！" :
                                accountInfo.AccountGroupInfo == null ? string.Empty : accountInfo.AccountGroupInfo.Name});

                    if (accountInfo != null && accountInfo.ActiveStatus == ActiveStatus.AlreadyActive)
                        item.Tag = accountInfo;
                    else
                        item.Tag = userInfo;


                    //ListViewItem item = new ListViewItem(new string[] { userInfo.uid, userInfo.real_name });
                    //item.Tag = userInfo;
                    listViewItems.Add(item);
                }
            }
            this.listView1.Items.AddRange(GetArray<ListViewItem>.Get(listViewItems));
        }


        private void FriendsManage_Load(object sender, EventArgs e)
        {
            this.Text = string.Format("好友管理：{0}", string.Format("{0}，{1}", _accountInfo.UserInfo.uid, _accountInfo.UserInfo.real_name));
            if (_accountInfo.FriendInfos != null)
            {
                form_load_display_friends_flag = true;
                allFriends = _accountInfo.FriendInfos;
                FillListView(_accountInfo.FriendInfos);
                _ownerForm.AppendText(string.Format("[{0}]：读取本地缓存获取[{1}]好友列表，共{2}位好友。\n",
                    DateTime.Now.ToString("HH:mm:ss"),
                    string.Format("{0},{1}", _accountInfo.UserInfo.uid, _accountInfo.UserInfo.real_name), _accountInfo.FriendInfos.Count.ToString()));

                Thread t = new Thread(new ThreadStart(LoadUserGroups));
                t.Start();
                lstThread.Add(t);
            }
            if (_sendCarInfo != null)
            {
                this.Text = "选择送车好友";
                this.label2.Text = string.Format("已选择送车：{0}", string.Format("{0}，{1}", _sendCarInfo.car_name, _sendCarInfo.price.ToString()));
                button4_Click(this.button4, EventArgs.Empty);
            }
            if (!string.IsNullOrEmpty(Send_Msg_Str))
            {
                this.label2.Text = "当前：给好友发消息为我的拉力赛加油";  // string.Format("已选择送车：{0}", string.Format("{0}，{1}", _sendCarInfo.car_name, _sendCarInfo.price.ToString()));
            }
        }

        private void ddlgroup_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.ddlgroup.SelectedIndex != 0)
            {
                User user = new User();
                if (user.Login(_accountInfo))
                {
                    FillListView(user.GetUserInfosByGroup(this.ddlgroup.Text).Where(u => u.uid != _accountInfo.UserInfo.uid).ToList());
                }
            }
            else
            {
                if(!form_load_display_friends_flag)
                    FillListView(allFriends);   
            }
            form_load_display_friends_flag = false;
        }

        void SendCar(object obj)
        {
            this.Invoke(new DSetControlDisabled(SetControlDisabled));

            SimpleUserInfo giveCarUserInfo = (SimpleUserInfo)obj;

            User user = new User();

            this.Invoke(new DAppendText(_ownerForm.AppendText), (string.Format("[{0}]：[{1}]开始登录系统……",
                DateTime.Now.ToString("HH:mm:ss"),
                string.Format("{0}", _accountInfo.Email))));

            if (user.Login(_accountInfo))
            {
                this.Invoke(new DAppendText(_ownerForm.AppendText),
                  string.Format("[{0}]：[{1}]成功登录系统！",
                      DateTime.Now.ToString("HH:mm:ss"),
                        string.Format("{0},{1}", _accountInfo.UserInfo.uid, _accountInfo.UserInfo.real_name)));

                string verify = user.GetVerify();

                this.Invoke(new DAppendText(_ownerForm.AppendText),
                  string.Format("[{0}]：获取VerifyCode：{1}\n",
                      DateTime.Now.ToString("HH:mm:ss"),
                        string.Format("{0}", verify)));

                this.Invoke(new DAppendText(_ownerForm.AppendText),
                    string.Format("[{0}]：[{1}]获取挣车位数据：",
                        DateTime.Now.ToString("HH:mm:ss"),
                        string.Format("{0},{1}", _accountInfo.UserInfo.uid, _accountInfo.UserInfo.real_name)));

                ParkInfo sendCarUserParkInfo = Park.GetParkInfoByUid(verify, _accountInfo.UserInfo.uid);

                this.Invoke(new DAppendText(_ownerForm.AppendText),
                    string.Format("[{0}]：[{1}]挣车位数据：{2}\n",
                        DateTime.Now.ToString("HH:mm:ss"),
                        string.Format("{0},{1}", _accountInfo.UserInfo.uid, _accountInfo.UserInfo.real_name),
                        string.Format("车({0})：{1} 现金：{2} 总金额：{3}",
                            sendCarUserParkInfo.CarInfos.Count.ToString(),
                            sendCarUserParkInfo.ParkAmountInfo.AllCarAmount.ToString(),
                            sendCarUserParkInfo.ParkAmountInfo.Cash.ToString(),
                            sendCarUserParkInfo.ParkAmountInfo.TotalAmount.ToString())));

                if (sendCarUserParkInfo.CarInfos.Count == 1)
                {
                    this.Invoke(new DAppendText(_ownerForm.AppendText),
                        string.Format("[{0}]：[{1}]只拥有1辆车,不能送车！\n",
                            DateTime.Now.ToString("HH:mm:ss"),
                            string.Format("{0},{1}", _accountInfo.UserInfo.uid, _accountInfo.UserInfo.real_name)));
                    MessageBox.Show("您只拥有1辆车,不能送车！");
                }
                else
                {
                    this.Invoke(new DAppendText(_ownerForm.AppendText),
                        string.Format("[{0}]：[{1}]获取挣车位数据：",
                            DateTime.Now.ToString("HH:mm:ss"),
                            string.Format("{0},{1}", giveCarUserInfo.uid, giveCarUserInfo.real_name)));

                    ParkInfo giveCarUserParkInfo = Park.GetParkInfoByUid(verify, giveCarUserInfo.uid);

                    this.Invoke(new DAppendText(_ownerForm.AppendText),
                        string.Format("[{0}]：[{1}]挣车位数据：{2}\n",
                            DateTime.Now.ToString("HH:mm:ss"),
                            string.Format("{0},{1}", giveCarUserInfo.uid, giveCarUserInfo.real_name),
                            string.Format("车({0})：{1} 现金：{2} 总金额：{3}",
                                giveCarUserParkInfo.CarInfos.Count.ToString(),
                                giveCarUserParkInfo.ParkAmountInfo.AllCarAmount.ToString(),
                                giveCarUserParkInfo.ParkAmountInfo.Cash.ToString(),
                                giveCarUserParkInfo.ParkAmountInfo.TotalAmount.ToString())));

                    if (giveCarUserParkInfo.CarInfos.Count < 10)
                    {
                        DisplaySendCar displaySendCar = new DisplaySendCar(sendCarUserParkInfo.CarInfos,_sendCarInfo, giveCarUserInfo);
                        if (displaySendCar.ShowDialog() == DialogResult.OK)
                        {
                            string message = Park.SendCar(verify, displaySendCar.carid, giveCarUserInfo.uid, displaySendCar.verifyCode.rcode, displaySendCar.code, displaySendCar.verifyCode.cc);
                            this.Invoke(new DAppendText(_ownerForm.AppendText),
                                string.Format("[{0}]：[{1}]：{2}\n",
                                    DateTime.Now.ToString("HH:mm:ss"),
                                    string.Format("{0},{1}", giveCarUserInfo.uid, giveCarUserInfo.real_name),
                                    message));
                            MessageBox.Show(message);
                        }
                    }
                    else
                    {
                        this.Invoke(new DAppendText(_ownerForm.AppendText),
                            string.Format("[{0}]：[{1}]已经拥有10辆车，不能送车！\n",
                                DateTime.Now.ToString("HH:mm:ss"),
                                string.Format("{0},{1}", giveCarUserInfo.uid, giveCarUserInfo.real_name)));

                        MessageBox.Show("对方已经拥有10辆车，不能送车！");
                    }
                }
            }
            this.Invoke(new DSetControlDisabled(RestoreControlEnabled));
        }

        delegate void DSetControlDisabled();
        void SetControlDisabled()
        {
            this.button1.Enabled = false;
            this.button3.Enabled = false;
            this.button4.Enabled = false;
            this.button5.Enabled = false;
            this.ddlgroup.Enabled = false;
            this.listView1.Enabled = false;
            this.button2.Enabled = false;
            this.button6.Enabled = false;
            this.button7.Enabled = false;
            this.button8.Enabled = false;
            this.button9.Enabled = false;
            this.button10.Enabled = false;
        }

        delegate void DRestoreControlEnabled();
        void RestoreControlEnabled()
        {
            this.button1.Enabled = true;
            this.button3.Enabled = true;
            this.button4.Enabled = true;
            this.button5.Enabled = true;
            this.ddlgroup.Enabled = true;
            this.listView1.Enabled = true;
            this.button2.Enabled = true;
            this.button6.Enabled = true;
            this.button7.Enabled = true;
            this.button8.Enabled = true;
            this.button9.Enabled = true;
            this.button10.Enabled = true;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            IList<SimpleUserInfo> userInfos = new List<SimpleUserInfo>();

            for (int i = 0; i < this.listView1.Items.Count; i++)
            {
                if (this.listView1.Items[i].Checked)
                {
                    //SimpleUserInfo userInfo = (SimpleUserInfo)this.listView1.Items[i].Tag;
                    userInfos.Add(GetUserInfoFromListViewItem(this.listView1.Items[i]));// userInfo);
                }
            }
            if (userInfos.Count == 0)
            {
                MessageBox.Show("尚未选择项！");
                return; 
            }
            if (userInfos.Count > 1)
            {
                MessageBox.Show("只能选择一个好友送车！");
                return; 
            }

            Thread t = new Thread(new ParameterizedThreadStart(SendCar));
            t.Start(userInfos[0]);
            lstThread.Add(t);
        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            for (int i = 0; i < this.listView1.Items.Count; i++)
            {
                this.listView1.Items[i].Checked = this.checkBox1.Checked;
            }

        }

        private void listView1_ItemCheck(object sender, ItemCheckEventArgs e)
        {
            //if (!can_more_check_flag)
            //{
            //    for (int i = 0; i < this.listView1.Items.Count; i++)
            //    {
            //        ListViewItem listViewItem = this.listView1.Items[i];
            //        if (i != e.Index)
            //            listViewItem.Checked = false;
            //    }
            //}
        }

        private void 删除好友ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (this.listView1.SelectedItems.Count > 0)
            {
                SimpleUserInfo userInfo = GetUserInfoFromListViewItem(this.listView1.SelectedItems[0]);// (SimpleUserInfo)this.listView1.SelectedItems[0].Tag;
                if (MessageBox.Show(string.Format("确定删除：{0}？", userInfo.real_name), "请确定", MessageBoxButtons.OKCancel) == DialogResult.OK)
                {
                    IList<SimpleUserInfo> userInfos = new List<SimpleUserInfo>();

                    userInfos.Add(userInfo);

                    Thread t = new Thread(new ParameterizedThreadStart(DeleteFriend));
                    t.Start(userInfos);
                    lstThread.Add(t);
                }
            }
        }

        private void 送车ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (this.listView1.SelectedItems.Count > 0)
            {
                SimpleUserInfo userInfo = GetUserInfoFromListViewItem(this.listView1.SelectedItems[0]); 

                Thread t = new Thread(new ParameterizedThreadStart(SendCar));
                t.Start(userInfo);
                lstThread.Add(t);
            }
        }

        private void listView1_MouseClick(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                if (this.listView1.SelectedItems.Count > 0)
                {
                    if (this.listView1.SelectedItems[0].Tag.GetType() == typeof(AccountInfo))
                    {
                        toolStripMenuItem1.Enabled = true;
                        toolStripMenuItem2.Enabled = true;
                        用他的账户打开浏览器登录开心网ToolStripMenuItem.Enabled = true;
                    }
                    else
                    {
                        toolStripMenuItem1.Enabled = false;
                        toolStripMenuItem2.Enabled = false;
                        用他的账户打开浏览器登录开心网ToolStripMenuItem.Enabled = false;
                    }

                    this.contextMenuStrip1.Show((Control)sender, new Point(e.X, e.Y));
                }
            }

        }

        private void toolStripMenuItem1_Click(object sender, EventArgs e)
        {
            if (this.listView1.SelectedItems.Count > 0)
            {
                if (this.listView1.SelectedItems[0].Tag.GetType() == typeof(AccountInfo))
                {
                    AccountInfo accountInfo = (AccountInfo)this.listView1.SelectedItems[0].Tag;
                    if (accountInfo.ActiveStatus == ActiveStatus.AlreadyActive)
                    {
                        MyCar myCar = new MyCar(_ownerForm, accountInfo);
                        myCar.ShowDialog();
                    }
                    //Thread t = new Thread(new ParameterizedThreadStart(SendCar));
                    //t.Start(userInfo);
                    //lstThread.Add(t);
                }
            }
        }

        delegate void DLoadParkMoney();
        void LoadParkMoney()
        {
            if (allFriends.Count > 0)
            {
                this.Invoke(new DSetControlDisabled(SetControlDisabled));

                User user = new User();

                this.Invoke(new DAppendText(_ownerForm.AppendText), (string.Format("[{0}]：[{1}]开始登录系统……",
                    DateTime.Now.ToString("HH:mm:ss"),
                    string.Format("{0}", _accountInfo.Email))));

                if (user.Login(_accountInfo))
                {
                    this.Invoke(new DAppendText(_ownerForm.AppendText),
                      string.Format("[{0}]：[{1}]成功登录系统！",
                          DateTime.Now.ToString("HH:mm:ss"),
                            string.Format("{0},{1}", _accountInfo.UserInfo.uid, _accountInfo.UserInfo.real_name)));

                    string verify = user.GetVerify();

                    this.Invoke(new DAppendText(_ownerForm.AppendText),
                      string.Format("[{0}]：获取VerifyCode：{1}\n",
                          DateTime.Now.ToString("HH:mm:ss"),
                            string.Format("{0}", verify)));

                    this.Invoke(new DAppendText(_ownerForm.AppendText),
                        string.Format("[{0}]：[{1}]获取挣车位数据：",
                            DateTime.Now.ToString("HH:mm:ss"),
                            string.Format("{0},{1}", _accountInfo.UserInfo.uid, _accountInfo.UserInfo.real_name)));

                    StringBuilder sbStatisticsParkMoney = new StringBuilder();
                    sbStatisticsParkMoney.AppendLine("uid\t姓名\t车数\t车资\t现金\t总资产");

                    for (int i = 0; i < allFriends.Count; i++)
                    {
                        SimpleUserInfo userInfo = allFriends[i];

                        ParkInfo parkInfo = Park.GetParkInfoByUid(verify, userInfo.uid);

                        this.Invoke(new DAppendText(_ownerForm.AppendText),
                            string.Format("[{0}]：({1})[{2}]：车({3})：{4} 现金：{5} 总资产：{6}",
                                DateTime.Now.ToString("HH:mm:ss"),
                                string.Format("{0}/{1}", (i + 1).ToString(), allFriends.Count.ToString()),
                                string.Format("{0},{1}", userInfo.uid, userInfo.real_name),
                                parkInfo.CarInfos.Count.ToString(), parkInfo.ParkAmountInfo.AllCarAmount.ToString(),
                                parkInfo.ParkAmountInfo.Cash.ToString(), parkInfo.ParkAmountInfo.TotalAmount.ToString()));

                        sbStatisticsParkMoney.AppendLine(string.Format("{0}\t{1}\t{2}\t{3}\t{4}\t{5}",
                            userInfo.uid,
                            userInfo.real_name,
                            parkInfo.CarInfos.Count.ToString(),
                            parkInfo.ParkAmountInfo.AllCarAmount.ToString(),
                            parkInfo.ParkAmountInfo.Cash.ToString(),
                            parkInfo.ParkAmountInfo.TotalAmount.ToString()));

                        if (i + 1 < allFriends.Count)
                            Thread.Sleep(_ownerForm.delayConnect * 1000);
                    }

                    user.Logout();
                    this.Invoke(new DAppendText(_ownerForm.AppendText),
                        string.Format("\n[{0}]：[{1}]退出系统！\n",
                            DateTime.Now.ToString("HH:mm:ss"),
                            string.Format("{0},{1}", _accountInfo.UserInfo.uid, _accountInfo.UserInfo.real_name)));

                    string statisticsParkMoneyfile = Path.Combine(Application.StartupPath, "车资统计.xlt");

                    using (StreamWriter sw = new StreamWriter(statisticsParkMoneyfile, false, Encoding.Default))
                    {
                        sw.WriteLine(sbStatisticsParkMoney.ToString());
                    }
                    this.Invoke(new DAppendText(_ownerForm.AppendText),
                        string.Format("[{0}]：统计挣车位金钱完成，并生成车资统计报表：{1}！",
                            DateTime.Now.ToString("HH:mm:ss"),
                            statisticsParkMoneyfile));

                }
                this.Invoke(new DSetControlDisabled(RestoreControlEnabled));

            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            Thread t = new Thread(new ThreadStart(LoadParkMoney));
            t.Start();
            lstThread.Add(t);

        }

        private void 送礼ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (this.listView1.SelectedItems.Count > 0)
            {
                SimpleUserInfo userInfo = GetUserInfoFromListViewItem(this.listView1.SelectedItems[0]);
                IList<SimpleUserInfo> userInfos = new List<SimpleUserInfo>();
                userInfos.Add(userInfo);
                selectUserInfos = userInfos;
                SendGift sendGift = new SendGift();
                if (sendGift.ShowDialog() == DialogResult.OK)
                {
                    Thread t = new Thread(new ParameterizedThreadStart(SendGift));
                    t.Start(sendGift.SendGiftInfo);
                    lstThread.Add(t);
                }
            }
        }

        void SendGift(object obj)
        {
            SendGiftInfo sendGiftInfo = (SendGiftInfo)obj;

            this.Invoke(new DSetControlDisabled(SetControlDisabled));

            User user = new User();

            this.Invoke(new DAppendText(_ownerForm.AppendText), (string.Format("[{0}]：[{1}]送礼：\n",
                DateTime.Now.ToString("HH:mm:ss"),
                string.Format("{0}", _accountInfo.Email))));


            this.Invoke(new DAppendText(_ownerForm.AppendText), (string.Format("[{0}]：[{1}]开始登录系统……",
                DateTime.Now.ToString("HH:mm:ss"),
                string.Format("{0}", _accountInfo.Email))));

            if (user.Login(_accountInfo))
            {
                this.Invoke(new DAppendText(_ownerForm.AppendText),
                  string.Format("[{0}]：[{1}]成功登录系统！\n",
                      DateTime.Now.ToString("HH:mm:ss"),
                        string.Format("{0},{1}", _accountInfo.UserInfo.uid, _accountInfo.UserInfo.real_name)));

                this.Invoke(new DAppendText(_ownerForm.AppendText), (string.Format("[{0}]：[{1}]查询剩余高级礼物数……",
                    DateTime.Now.ToString("HH:mm:ss"),
                    string.Format("{0}", _accountInfo.Email))));

                int advgiftcount = 0;
                IList<GiftInfo> giftInfos = new List<GiftInfo>();
                IList<GiftInfo> commonGiftInfos = new List<GiftInfo>();
                IList<GiftInfo> advGiftInfos = new List<GiftInfo>();

                giftInfos = user.GetGiftInfos(ref advgiftcount);
                for (int i = 0; i < giftInfos.Count; i++)
                {
                    GiftInfo giftInfo = giftInfos[i];
                    if (giftInfo.GiftType == GiftType.普通礼物)
                        commonGiftInfos.Add(giftInfo);
                    else if (giftInfo.GiftType == GiftType.高级礼物)
                        advGiftInfos.Add(giftInfo);
                }
                this.Invoke(new DAppendText(_ownerForm.AppendText), (string.Format("[{0}]：[{1}]剩余高级礼物数：{2}\n",
                    DateTime.Now.ToString("HH:mm:ss"),
                    string.Format("{0}", _accountInfo.Email),
                    advgiftcount.ToString())));

                string giftname = string.Empty;
                int gid = sendGiftInfo.gid;

                bool is_adv_gift = advGiftInfos.Where(g => g.Id == gid).Count() > 0;
                bool is_common_gift = commonGiftInfos.Where(g => g.Id == gid).Count() > 0;

                if(is_adv_gift)
                    giftname = advGiftInfos.Single(g => g.Id == gid).Name;
                else if(is_common_gift)
                    giftname = commonGiftInfos.Single(g => g.Id == gid).Name;

                bool can_send_more_advgift_flag = (sendGiftInfo.randomgifttype == 1) || is_adv_gift;

                for (int i = 0; i < selectUserInfos.Count; i++)
                {
                    int giftnum = can_send_more_advgift_flag ? sendGiftInfo.advgiftnum : 1;
                    for (int j = 0; j < giftnum; j++)
                    {
                        if (sendGiftInfo.randomgifttype == 0)   //普通礼物随机
                        {
                            int randomNum = new Random().Next(0, commonGiftInfos.Count);
                            gid = commonGiftInfos[randomNum].Id;
                            giftname = commonGiftInfos[randomNum].Name;
                        }
                        else if (sendGiftInfo.randomgifttype == 1)   //高级礼物随机
                        {
                            int randomNum = new Random().Next(0, advGiftInfos.Count);
                            gid = advGiftInfos[randomNum].Id;
                            giftname = advGiftInfos[randomNum].Name;
                        }

                        this.Invoke(new DAppendText(_ownerForm.AppendText),
                          string.Format("[{0}]：向[{1}]送礼：[{2}]……",
                              DateTime.Now.ToString("HH:mm:ss"),
                                string.Format("{0},{1}", selectUserInfos[i].uid, selectUserInfos[i].real_name),
                                string.Format("{0},{1}", gid.ToString(),giftname)));

                        string send_gift_message = user.SendGift(selectUserInfos[i].uid, gid, sendGiftInfo.quiet, sendGiftInfo.anon, sendGiftInfo.content, sendGiftInfo.texttype);
                        this.Invoke(new DAppendText(_ownerForm.AppendText),
                          string.Format("[{0}]：{1} 进度：{2}\n",
                              DateTime.Now.ToString("HH:mm:ss"),
                                send_gift_message,
                                string.Format("{0}/{1}(好友) {2}/{3}(礼物)", (i + 1).ToString(), selectUserInfos.Count.ToString(), (j + 1).ToString(), giftnum)));

                        if (send_gift_message == "抱歉！不能再赠送高级礼物了。")
                            goto exit;

                        if (j + 1 < giftnum)
                            Thread.Sleep(_ownerForm.delayConnect * 1000);
                    }
                    if (i + 1 < selectUserInfos.Count)
                        Thread.Sleep(_ownerForm.delayConnect * 1000);
                }
exit:           user.Logout();
                this.Invoke(new DAppendText(_ownerForm.AppendText),
                    string.Format("[{0}]：[{1}]退出系统！\n",
                        DateTime.Now.ToString("HH:mm:ss"),
                        string.Format("{0},{1}", _accountInfo.UserInfo.uid, _accountInfo.UserInfo.real_name)));

                this.Invoke(new DAppendText(_ownerForm.AppendText), (string.Format("[{0}]：[{1}]完成送礼！\n",
                    DateTime.Now.ToString("HH:mm:ss"),
                    string.Format("{0}", _accountInfo.Email))));

            }

            this.Invoke(new DSetControlDisabled(RestoreControlEnabled));

        }

        private void button6_Click(object sender, EventArgs e)
        {
            IList<SimpleUserInfo> userInfos = new List<SimpleUserInfo>();

            for (int i = 0; i < this.listView1.Items.Count; i++)
            {
                if (this.listView1.Items[i].Checked)
                {
                    userInfos.Add(GetUserInfoFromListViewItem(this.listView1.Items[i]));
                }
            }
            if (userInfos.Count == 0)
            {
                MessageBox.Show("尚未选择项！");
                return;
            }
            selectUserInfos = userInfos;
            SendGift sendGift = new SendGift();
            if (sendGift.ShowDialog() == DialogResult.OK)
            {
                Thread t = new Thread(new ParameterizedThreadStart(SendGift));
                t.Start(sendGift.SendGiftInfo);
                lstThread.Add(t);
            }
        }

        private void button7_Click(object sender, EventArgs e)
        {
            IList<SimpleUserInfo> userInfos = new List<SimpleUserInfo>();

            for (int i = 0; i < this.listView1.Items.Count; i++)
            {
                if (this.listView1.Items[i].Checked)
                {
                    userInfos.Add(GetUserInfoFromListViewItem(this.listView1.Items[i]));
                }
            }
            if (userInfos.Count == 0)
            {
                MessageBox.Show("尚未选择项！");
                return;
            }
            if (MessageBox.Show("确定举报选择好友的汽车吗？","请确定",MessageBoxButtons.OKCancel) == DialogResult.OK)
            {
                Thread t = new Thread(new ParameterizedThreadStart(DoCall));
                t.Start(userInfos);
                lstThread.Add(t);
            }
        }

        void DoCall(object obj)
        {
            this.Invoke(new DSetControlDisabled(SetControlDisabled));

            IList<SimpleUserInfo> userInfos = (List<SimpleUserInfo>)obj;

            User user = new User();

            this.Invoke(new DAppendText(_ownerForm.AppendText), (string.Format("[{0}]：[{1}]举报：\n",
                DateTime.Now.ToString("HH:mm:ss"),
                string.Format("{0}", _accountInfo.Email))));


            this.Invoke(new DAppendText(_ownerForm.AppendText), (string.Format("[{0}]：[{1}]开始登录系统……",
                DateTime.Now.ToString("HH:mm:ss"),
                string.Format("{0}", _accountInfo.Email))));

            if (user.Login(_accountInfo))
            {
                this.Invoke(new DAppendText(_ownerForm.AppendText),
                  string.Format("[{0}]：[{1}]成功登录系统！\n",
                      DateTime.Now.ToString("HH:mm:ss"),
                        string.Format("{0},{1}", _accountInfo.UserInfo.uid, _accountInfo.UserInfo.real_name)));

                string acc = string.Empty;
                string verify = user.GetVerify(ref acc);

                this.Invoke(new DAppendText(_ownerForm.AppendText),
                  string.Format("[{0}]：[{1}]获取acc：{2}|verift：{3}！\n",
                      DateTime.Now.ToString("HH:mm:ss"),
                        string.Format("{0},{1}", _accountInfo.UserInfo.uid, _accountInfo.UserInfo.real_name),
                        acc,verify));

                for (int i = 0; i<userInfos.Count; i++)
                {
                    SimpleUserInfo simpleUserInfo = userInfos[i];

                    this.Invoke(new DAppendText(_ownerForm.AppendText),
                      string.Format("[{0}]：[{1}]读取汽车数据：",
                          DateTime.Now.ToString("HH:mm:ss"),
                            string.Format("{0},{1}", simpleUserInfo.uid, simpleUserInfo.real_name)));

                    ParkInfo parkInfo = Park.GetParkInfoByUid(verify, simpleUserInfo.uid);

                    this.Invoke(new DAppendText(_ownerForm.AppendText),
                      string.Format("[{0}]：[{1}]汽车数据：{2}\n",
                          DateTime.Now.ToString("HH:mm:ss"),
                            string.Format("{0},{1}", simpleUserInfo.uid, simpleUserInfo.real_name),
                            string.Format("车({0})：{1} 现金：{2} 总金额：{3}",
                                parkInfo.CarInfos.Count.ToString(),
                                parkInfo.ParkAmountInfo.AllCarAmount.ToString(),
                                parkInfo.ParkAmountInfo.Cash.ToString(),
                                parkInfo.ParkAmountInfo.TotalAmount.ToString())));

                    this.Invoke(new DAppendText(_ownerForm.AppendText),
                      string.Format("[{0}]：[{1}]开始举报：[{2}]",
                          DateTime.Now.ToString("HH:mm:ss"),
                            string.Format("{0},{1}", _accountInfo.UserInfo.uid, _accountInfo.UserInfo.real_name),
                            string.Format("{0},{1}", simpleUserInfo.uid, simpleUserInfo.real_name)));

                    for (int j = 0; j < parkInfo.CarInfos.Count; j++)
                    {
                        CarInfo carInfo = parkInfo.CarInfos[j];
                        if (carInfo.neighbor == "1" || carInfo.neighbor == "2")
                        {
                            //邻居车位不能举报
                            this.Invoke(new DAppendText(_ownerForm.AppendText),
                              string.Format("[{0}]：[{1}] #{2}：{3}",
                                  DateTime.Now.ToString("HH:mm:ss"),
                                    string.Format("{0},{1}", _accountInfo.UserInfo.uid, _accountInfo.UserInfo.real_name),
                                    (j + 1).ToString(),
                                    string.Format("{0} 他（她）的[{1}]停在邻居车位上，无法举报！！", simpleUserInfo.real_name, carInfo.car_name)));
                        }
                        else
                        {
                            if (carInfo.park_uid != 0)
                            {
                                string message = Park.DoCall(acc, verify, carInfo.park_uid.ToString(), carInfo.parkid.ToString());

                                message = string.IsNullOrEmpty(message) ? "举报出错" : message;

                                this.Invoke(new DAppendText(_ownerForm.AppendText),
                                  string.Format("[{0}]：[{1}] #{2}：{3}",
                                      DateTime.Now.ToString("HH:mm:ss"),
                                        string.Format("{0},{1}", _accountInfo.UserInfo.uid, _accountInfo.UserInfo.real_name),
                                        (j + 1).ToString(),
                                        string.Format("{0}的[{1}]停在[{2}]的车位了,举报！！[{3}]", simpleUserInfo.real_name, carInfo.car_name, carInfo.park_real_name,message)));

                                if (j + 1 < parkInfo.CarInfos.Count)
                                    Thread.Sleep(_ownerForm.delayConnect * 1000);
                            }
                            else
                            {
                                //没有停在好友车位上
                                this.Invoke(new DAppendText(_ownerForm.AppendText),
                                  string.Format("[{0}]：[{1}] #{2}：{3}",
                                      DateTime.Now.ToString("HH:mm:ss"),
                                        string.Format("{0},{1}", _accountInfo.UserInfo.uid, _accountInfo.UserInfo.real_name),
                                        (j + 1).ToString(),
                                        string.Format("{0}的[{1}]没有停在好友车位上，无法举报！！", simpleUserInfo.real_name, carInfo.car_name)));
                            }
                        }
                    }

                    this.Invoke(new DAppendText(_ownerForm.AppendText),
                      string.Format("[{0}]：[{1}]举报：[{2}]完成！ 进度：{3}\n",
                          DateTime.Now.ToString("HH:mm:ss"),
                            string.Format("{0},{1}", _accountInfo.UserInfo.uid, _accountInfo.UserInfo.real_name),
                            string.Format("{0},{1}", simpleUserInfo.uid, simpleUserInfo.real_name),
                            string.Format("{0}/{1}", (i + 1).ToString(), userInfos.Count.ToString())));

                }

                user.Logout();
                this.Invoke(new DAppendText(_ownerForm.AppendText),
                    string.Format("[{0}]：[{1}]退出系统！\n",
                        DateTime.Now.ToString("HH:mm:ss"),
                        string.Format("{0},{1}", _accountInfo.UserInfo.uid, _accountInfo.UserInfo.real_name)));

            }

            this.Invoke(new DAppendText(_ownerForm.AppendText),
                string.Format("[{0}]：[{1}]完成举报！\n",
                    DateTime.Now.ToString("HH:mm:ss"),
                    string.Format("{0},{1}", _accountInfo.UserInfo.uid, _accountInfo.UserInfo.real_name)));
            this.Invoke(new DSetControlDisabled(RestoreControlEnabled));

        }

        private void toolStripMenuItem2_Click(object sender, EventArgs e)
        {
            if (this.listView1.SelectedItems.Count > 0)
            {
                if (this.listView1.SelectedItems[0].Tag.GetType() == typeof(AccountInfo))
                {
                    AccountInfo accountInfo = (AccountInfo)this.listView1.SelectedItems[0].Tag;
                    if (accountInfo.ActiveStatus == ActiveStatus.AlreadyActive)
                    {
                        FriendsManage friendsManage = new FriendsManage(_ownerForm, accountInfo);
                        friendsManage.ShowDialog();
                    }
                }
            }

        }

        private void 用他的账户打开浏览器登录开心网ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (this.listView1.SelectedItems.Count > 0)
            {
                if (this.listView1.SelectedItems[0].Tag.GetType() == typeof(AccountInfo))
                {
                    AccountInfo accountInfo = (AccountInfo)this.listView1.SelectedItems[0].Tag;
                    if (accountInfo.ActiveStatus == ActiveStatus.AlreadyActive)
                    {
                        _ownerForm.ProcessLoginKaiXin001(accountInfo);
                    }
                }
            }
        }

        private void button8_Click(object sender, EventArgs e)
        {
            IList<SimpleUserInfo> userInfos = new List<SimpleUserInfo>();

            for (int i = 0; i < this.listView1.Items.Count; i++)
            {
                if (this.listView1.Items[i].Checked)
                {
                    userInfos.Add(GetUserInfoFromListViewItem(this.listView1.Items[i]));
                }
            }
            if (userInfos.Count == 0)
            {
                MessageBox.Show("尚未选择项！");
                return;
            }
            if (MessageBox.Show(string.Format("确定删除选择的{0}位好友？\n本功能为级联删除，即如选择的（某些）账户也通过本工具管理，系统将自动登录该账户将其所有好友删除，使用时请慎重！！！", userInfos.Count.ToString()), "请确定", MessageBoxButtons.OKCancel) == DialogResult.OK)
            {
                Thread t = new Thread(new ParameterizedThreadStart(IterationDeleteFriend));
                t.Start(userInfos);
                lstThread.Add(t);
            }

        }

        private void button9_Click(object sender, EventArgs e)
        {
            msg_groupsend_flag = false;
            SendMsg();
            
        }

        void SendMsg()
        {
            IList<SimpleUserInfo> userInfos = new List<SimpleUserInfo>();

            for (int i = 0; i < this.listView1.Items.Count; i++)
            {
                if (this.listView1.Items[i].Checked)
                {
                    userInfos.Add(GetUserInfoFromListViewItem(this.listView1.Items[i]));
                }
            }
            if (userInfos.Count == 0)
            {
                MessageBox.Show("尚未选择项！");
                return;
            }
            selectUserInfos = userInfos;
            Msg msg = new Msg();
            msg.Send_Msg_Str = Send_Msg_Str;
            if (msg.ShowDialog() == DialogResult.OK)
            {
                Thread t = new Thread(new ParameterizedThreadStart(Msg));
                t.Start(msg.MsgInfo);
                lstThread.Add(t);
            }
        }

        void Msg(object obj)
        {
            this.Invoke(new DSetControlDisabled(SetControlDisabled));

            MsgInfo msgInfo = (MsgInfo)obj;

            User user = new User();

            this.Invoke(new DAppendText(_ownerForm.AppendText), (string.Format("[{0}]：[{1}]发送短消息：\n",
                DateTime.Now.ToString("HH:mm:ss"),
                string.Format("{0}", _accountInfo.Email))));


            this.Invoke(new DAppendText(_ownerForm.AppendText), (string.Format("[{0}]：[{1}]开始登录系统……",
                DateTime.Now.ToString("HH:mm:ss"),
                string.Format("{0}", _accountInfo.Email))));

            if (user.Login(_accountInfo))
            {
                this.Invoke(new DAppendText(_ownerForm.AppendText),
                  string.Format("[{0}]：[{1}]成功登录系统！\n",
                      DateTime.Now.ToString("HH:mm:ss"),
                        string.Format("{0},{1}", _accountInfo.UserInfo.uid, _accountInfo.UserInfo.real_name)));

                if (msg_groupsend_flag)
                {
                    this.Invoke(new DAppendText(_ownerForm.AppendText), (string.Format("[{0}]：[{1}]当前选择短消息群发模式：",
                        DateTime.Now.ToString("HH:mm:ss"),
                        string.Format("{0}", _accountInfo.Email))));

                    string uids = string.Empty;
                    for (int i = 0; i < selectUserInfos.Count; i++)
                    {
                        uids += string.Format("{0},", selectUserInfos[i].uid);
                    }
                    uids = uids.TrimEnd(',');

                    this.Invoke(new DAppendText(_ownerForm.AppendText),
                      string.Format("[{0}]：[{1}]向uid：{2}群发短消息：",
                          DateTime.Now.ToString("HH:mm:ss"),
                            string.Format("{0},{1}", _accountInfo.UserInfo.uid, _accountInfo.UserInfo.real_name),
                            uids));

                    user.SendMsg(msgInfo.content, msgInfo.texttype, uids);

                    this.Invoke(new DAppendText(_ownerForm.AppendText),
                      string.Format("[{0}]：[{1}]短消息群发完成！",
                          DateTime.Now.ToString("HH:mm:ss"),
                            string.Format("{0},{1}", _accountInfo.UserInfo.uid, _accountInfo.UserInfo.real_name)));

                }
                else
                {
                    for (int i = 0; i < selectUserInfos.Count; i++)
                    {
                        SimpleUserInfo simpleUserInfo = selectUserInfos[i];

                        user.SendMsg(msgInfo.content, msgInfo.texttype, simpleUserInfo.uid);

                        this.Invoke(new DAppendText(_ownerForm.AppendText),
                          string.Format("[{0}]：[{1}]向[{2}]群发短消息！",
                              DateTime.Now.ToString("HH:mm:ss"),
                                string.Format("{0},{1}", _accountInfo.UserInfo.uid, _accountInfo.UserInfo.real_name),
                                string.Format("{0},{1}", simpleUserInfo.uid, simpleUserInfo.real_name)));

                        if (i + 1 < selectUserInfos.Count)
                            Thread.Sleep(_ownerForm.delayConnect * 1000);
                    }
                }

                user.Logout();
                this.Invoke(new DAppendText(_ownerForm.AppendText),
                    string.Format("[{0}]：[{1}]退出系统！\n",
                        DateTime.Now.ToString("HH:mm:ss"),
                        string.Format("{0},{1}", _accountInfo.UserInfo.uid, _accountInfo.UserInfo.real_name)));


                this.Invoke(new DAppendText(_ownerForm.AppendText),
                  string.Format("[{0}]：[{1}]发送短消息完成！",
                      DateTime.Now.ToString("HH:mm:ss"),
                        string.Format("{0},{1}", _accountInfo.UserInfo.uid, _accountInfo.UserInfo.real_name)));


            }
            this.Invoke(new DSetControlDisabled(RestoreControlEnabled));

        }

        private void button10_Click(object sender, EventArgs e)
        {
            msg_groupsend_flag = true;
            SendMsg();
        }
    }
}
